package com.sinosoft.brokerchannel.base.utile;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Calendar;
import java.util.Date;

/**
 * 日期处理组件类
 */
public class DateUtils {
    protected static Logger logger = LoggerFactory.getLogger(DateUtils.class);

    /**
     * 比较两日期的年份差
     *
     * @param src 日期1
     * @param dst 日期2
     * @return 年份差值
     * 描述：默认2008-05-04到2009-05-03不算一年
     */
    public static int compareYear(Date src, Date dst) {
        return compareYear(src, dst, true);
    }

    /**
     * 根据阈值field截取日期date 。
     * 例如， 如果你的时间是 28 Mar 2002 13:45:01.231，
     * 如果field为HOUR，它将返回 28 Mar 2002 13:00:00.000；
     * 如果field为MONTH，它将返回 1 Mar 2002 0:00:00.000 。
     *
     * @param date  处理的日期，非null
     * @param field 阈值
     */
    public static Calendar truncate(Calendar date, int field) {
        return org.apache.commons.lang.time.DateUtils.truncate(date, field);
    }

    /**
     * @param date
     * @param amount
     * @return
     * @方法描述:
     * @参数说明：Adds a number of days to a date returning a new object.
     */
    public static Date addDays(Date date, int amount) {
        return org.apache.commons.lang.time.DateUtils.addDays(date, amount);
    }

    /**
     * 比较两日期的年份差
     *
     * @param src       开始时间
     * @param dst       结束时间
     * @param todayFlag 是否包含当天[标志]
     * @return 年份差值
     * 描述：todayFlag=false，则2008-05-04到2009-05-03算一年
     */
    private static int compareYear(Date src, Date dst, boolean todayFlag) {
        Calendar srcCal = Calendar.getInstance();
        srcCal.setTime(src);
        Calendar dstCal = Calendar.getInstance();
        dstCal.setTime(dst);

        if (todayFlag)
            return dstCal.get(Calendar.YEAR) - srcCal.get(Calendar.YEAR) - ((dstCal.get(Calendar.MONTH) - srcCal.get(Calendar.MONTH) > 0) ? 0 : ((dstCal.get(Calendar.MONTH) - srcCal.get(Calendar.MONTH)) < 0 ? 1 : ((dstCal.get(Calendar.DAY_OF_MONTH) - srcCal.get(Calendar.DAY_OF_MONTH)) >= 0 ? 0 : 1)));
        else
            return dstCal.get(Calendar.YEAR) - srcCal.get(Calendar.YEAR) - ((dstCal.get(Calendar.MONTH) - srcCal.get(Calendar.MONTH) > 0) ? 0 : ((dstCal.get(Calendar.MONTH) - srcCal.get(Calendar.MONTH)) < 0 ? 1 : ((dstCal.get(Calendar.DAY_OF_MONTH) - srcCal.get(Calendar.DAY_OF_MONTH)) >= -1 ? 0 : 1)));
    }

    /**
     * 比较两日期的月份差
     *
     * @param src 日期1
     * @param dst 日期2
     * @return 月份差值
     */
    public static int compareMonth(Date src, Date dst) {
        Calendar srcCal = Calendar.getInstance();
        srcCal.setTime(src);
        Calendar dstCal = Calendar.getInstance();
        dstCal.setTime(dst);
        return (dstCal.get(Calendar.YEAR) - srcCal.get(Calendar.YEAR)) * 12 + //年差
                (dstCal.get(Calendar.MONTH) - srcCal.get(Calendar.MONTH)) - //月差
                ((dstCal.get(Calendar.DAY_OF_MONTH) - srcCal.get(Calendar.DAY_OF_MONTH)) >= 0 ? 0 : 1);
    }

    /**
     * 比较两日期的日期差
     *
     * @param src 日期1
     * @param dst 日期2
     * @return 日期差值
     */
    public static int compareDay(Date src, Date dst) {
        Calendar srcCal = Calendar.getInstance();
        srcCal.setTime(src);
        Calendar dstCal = Calendar.getInstance();
        dstCal.setTime(dst);
        return (int) ((dst.getTime() - src.getTime()) / 1000 / 60 / 60 / 24);
    }

    /**
     * 比较支票审核两日期的日期差
     *
     * @param src 日期1
     * @param dst 日期2
     * @return 日期差值
     */
    public static int compareCheckDay(Date src, Date dst) {
        Calendar srcCal = Calendar.getInstance();
        srcCal.setTime(src);
        Calendar dstCal = Calendar.getInstance();
        dstCal.setTime(dst);
        return (int) ((dst.getTime() - src.getTime()) / 1000 / 60 / 60 / 24) + 2;
    }

    /**
     * 时间1减去时间2
     *
     * @param date1
     * @param date2
     * @return 时间差值
     */
    public static int d1MinusD2(Date date1, Date date2) {
        Calendar d1 = Calendar.getInstance();
        Calendar d2 = Calendar.getInstance();
        d1.setTime(date1);
        d2.setTime(date2);
        if (d1.after(d2)) {  // swap dates so that d1 is start and d2 is end
            java.util.Calendar swap = d1;
            d1 = d2;
            d2 = swap;
        }
        int d1_totalDayOfYear = d1.getActualMaximum(Calendar.DAY_OF_YEAR);
        int d1_dayOfYear = d1.get(Calendar.DAY_OF_YEAR);
        int num1 = 0;
        num1 = d1_totalDayOfYear - d1_dayOfYear;
        int d2_totalDayOfYear = d2.getActualMaximum(Calendar.DAY_OF_YEAR);
        int d2_dayOfYear = d2.get(Calendar.DAY_OF_YEAR);
        int num2 = d2_totalDayOfYear - d2_dayOfYear;
        if (d1.get(Calendar.YEAR) == d2.get(Calendar.YEAR)) {
            return num1 - num2;
        } else {
            int count = num1 + d2_dayOfYear;
            d1.set(Calendar.YEAR, d1.get(Calendar.YEAR) + 1);
            while (d1.get(Calendar.YEAR) != d2.get(Calendar.YEAR)) {
                d1.set(Calendar.YEAR, d1.get(Calendar.YEAR) + 1);
                count = count + d1.getActualMaximum(Calendar.DAY_OF_YEAR);
            }
            return count;
        }
    }
}
